﻿using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;
using FlatRedBall.Graphics;
using FlatRedBall.Graphics.Model;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;

using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;
using FlatRedBall.Localization;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;
#endif

using NereusGame.Screens;
using NereusGame.Entities.Ships.Enemies;
using NereusGame.Entities.Ships;
using NereusGame.Entities.HBodies;
using NereusGame.Entities.Projectiles;
using FlatRedBall.Instructions;

namespace NereusGame.HUD
{
    public class BossHud
    {
        public const float MEDIUM_HEALTH = 0.70f;
        public const float CRITICAL_HEALTH = 0.25f;
        protected const string CONTEN_MANAGER = "NereusGameContent";   // Name of the contentManager
        private EnemyBoss boss;
        private Camera camera;
        private Sprite Body;
        private Sprite Healthbar;
        private float intialHealthBarScaleX;
        private Text bossHealth;
        private static Texture2D RedHP;// = FlatRedBallServices.Load<Texture2D>(@"content/Entities/HUDs/PlayerHUD/RedHP", CONTEN_MANAGER);
        private static Texture2D OrangeHP;// = FlatRedBallServices.Load<Texture2D>(@"content/Entities/HUDs/PlayerHUD/OrangeHP", CONTEN_MANAGER);
        private static Texture2D GreenHP;// = FlatRedBallServices.Load<Texture2D>(@"content/Entities/HUDs/PlayerHUD/GreenDot", CONTEN_MANAGER);



        // private BitmapFont customFont;
        // constructor
        public BossHud(Camera mCamera, EnemyBoss mBoss)
        {

            boss = mBoss;
            camera = mCamera;
            Body = new Sprite();
            Healthbar = new Sprite();
            SpriteManager.AddSprite(Body);
            Healthbar.AttachTo(Body, false);
            SpriteManager.AddSprite(Healthbar);
            SpriteManager.AddToLayer(Body, camera.Layer);
            SpriteManager.AddToLayer(Healthbar, camera.Layer);

            Body.AttachTo(camera, false);
           // Body.Texture = FlatRedBallServices.Load<Texture2D>(@"content/Entities/HUDs/PlayerHUD/Bar1", CONTEN_MANAGER);
            Body.Visible = true;
            SetBody();

            Healthbar.Texture = GreenHP;
            

            InitializeText();
            SetTexts();

        }


        private void InitializeText()
        {
            bossHealth = TextManager.AddText("");
            TextManager.AddToLayer(bossHealth, camera.Layer);
            bossHealth.AttachTo(Body, false);
            bossHealth.Visible = true;
           

            

        }
        // update the paused screen
        public void Update()
        {
            UpdateHPBar();

        }



        // remove sprites
        public void Destroy()
        {
            SpriteManager.RemoveSprite(Body);
            SpriteManager.RemoveSprite(Healthbar);
            TextManager.RemoveText(bossHealth);

        }


        public void SetCamera(Camera c)
        {
            camera.Layer.Remove(Body);
            camera.Layer.Remove(Healthbar);
            camera.Layer.Remove(bossHealth);
            Body.Detach();
            camera = c;
            Body.AttachTo(camera, false);
            SpriteManager.AddToLayer(Body, camera.Layer);
            SpriteManager.AddToLayer(Healthbar, camera.Layer);
            TextManager.AddToLayer(bossHealth, camera.Layer);
            SetBody();
            SetTexts();

        }

        private void SetBody()
        {
            Body.RelativePosition = new Vector3(0, camera.YEdge + 1f, -(camera.Z - 5));
            Body.ScaleY = camera.YEdge * 0.125f;
            Body.ScaleX = camera.XEdge * 1.16f;

            Healthbar.ScaleY = Body.ScaleY * 0.16f;
            intialHealthBarScaleX = Healthbar.ScaleX = Body.ScaleX * 0.3f;
            Healthbar.RelativePosition = new Vector3(-Body.ScaleX * 0.65f, 0, 1f);

            

        }

        private void SetTexts()
        {
            bossHealth.RelativePosition.X = -Body.ScaleX * 0.3f;
            bossHealth.RelativePosition.Z = 1f;


        }

        private void UpdateHPBar()
        {
            if (boss.CurrentHealth <= 0)
            {
                Healthbar.ScaleX = 0;
            }
            else
            {
                Healthbar.ScaleX = (boss.CurrentHealth / boss.MaxHealth) * intialHealthBarScaleX;
               // UpdateHealthBarColor();
     
            }
            UpdateHP();

        }

        private void UpdateHP()
        {
            int currentHP = (int)boss.CurrentHealth;
            if (currentHP < 0)
                currentHP = 0;
            bossHealth.DisplayText = "HP: " + currentHP.ToString() + " / " + boss.MaxHealth.ToString();
        }


        private void UpdateHealthBarColor()
        {
            if (boss.CurrentHealth <= boss.MaxHealth * CRITICAL_HEALTH)
                Healthbar.Texture = RedHP;
            else if (boss.CurrentHealth <= boss.MaxHealth * MEDIUM_HEALTH)
                Healthbar.Texture = OrangeHP;
            else
                Healthbar.Texture = GreenHP;
        }

        public void Hide()
        {
            Healthbar.Visible = false;
            Body.Visible = false;
        }

        public void Show()
        {
            Healthbar.Visible = true;
            Body.Visible = true;

        }
    }
}
